# -*- coding: utf-8 -*-

from django.shortcuts import render

from site1.methods.ga import ga_execute, ga_deliver

import numpy as np

import json

from django.views.decorators import csrf


# 接收POST请求数据
def search_post(request):
    global route_info
    input_info = {'NodesInfo': [], 'CarIsChangeAble': False, 'CarIsNotChangeAble': False, 'CarNumSet': 0,
                  'OneTypeCar': False, 'TwoTypeCar': False, 'Load_1': 0, 'Load_2': 0, 'CarSpeed': 0,
                  'UnloadTime': 0, 'TimeLimit': 0, 'LengthLimit': 0, 'GenerationsNumber': 20}
    if request.POST:
        print(request.POST)

        if int(request.POST['car_stable']) == 1:
            input_info['CarIsNotChangeAble'] = True
            input_info['CarIsChangeAble'] = False
        if int(request.POST['car_stable']) == 2:
            input_info['CarIsNotChangeAble'] = False
            input_info['CarIsChangeAble'] = True
        if int(request.POST['car_type']) == 1:
            input_info['OneTypeCar'] = True
            input_info['TwoTypeCar'] = False
        if int(request.POST['car_type']) == 2:
            input_info['OneTypeCar'] = False
            input_info['TwoTypeCar'] = True

        input_info['CarNumSet'] = int(request.POST['car_number'])
        input_info['Load_1'] = int(request.POST['car_load_1'])
        input_info['Load_2'] = int(request.POST['car_load_2'])
        input_info['CarSpeed'] = int(request.POST['car_speed'])

        input_info['UnloadTime'] = int(request.POST['unload_time'])
        input_info['TimeLimit'] = int(request.POST['time_limit'])
        input_info['LengthLimit'] = int(request.POST['length_limit'])

        print(input_info)

        deliver = ga_deliver.vrp_solution(input_info)
        input_type, route_info, route_time, route_load, route_length = deliver.vrp_execute()


        listx = []
        for i in range(0, len(route_info)):
            str = f"route{i}"
            listx.append(str)

        type_json = {'type': input_type}
        route_json = dict(zip(listx, route_info))
        time_json = dict(zip(listx, route_time))
        load_json = dict(zip(listx, route_load))
        length_json = dict(zip(listx, route_length))

        input_type = json.dumps(type_json)
        result_route = json.dumps(route_json)  # json转为string
        result_time = json.dumps(time_json)  # json转为string
        result_load = json.dumps(load_json)  # json转为string
        result_length = json.dumps(length_json)  # json转为string

        print(input_type)
        print(result_route)
        print(result_time)
        print(result_load)
        print(result_length)

    return render(request, "input.html", {
        'result_type': input_type, 'result_route': result_route,
        'result_time': result_time, 'result_load': result_load,
        'result_length': result_length
    })
